from flask import Flask, abort, render_template, make_response

app = Flask(__name__,template_folder='error_templates')

@app.route('/')
def index():
    abort(401)
    return "Hello flask!"

"""
127.0.0.1 - - [31/Aug/2021 21:57:04] "GET / HTTP/1.1" 401 -
"""

@app.errorhandler(401)
def error_401(error):
    # 方式一：
    #print(error)
    """
    401 Unauthorized: The server could not verify that you are authorized to access the URL requested. You either supplied the wrong credentials (e.g. a bad password), or your browser doesn't understand how to supply the credentials req
uired.
    """
    #return render_template('401.html'), 401
    # 方式二：
    resp = make_response(render_template('401.html'))
    resp.status_code = 401
    return resp
